package com.bank.backend.boot.server.service.impl;


import com.bank.backend.boot.base.model.dto.BankInfoDTO;
import com.bank.backend.boot.base.model.entity.BankBase;
import com.bank.backend.boot.base.model.entity.BankBranch;
import com.bank.backend.boot.base.model.entity.BankUser;
import com.bank.backend.boot.base.model.exception.GlobalException;
import com.bank.backend.boot.base.model.vo.BankUserVO;
import com.bank.backend.boot.base.utils.ThreadLocalUtil;
import com.bank.backend.boot.server.converter.BankUserConverter;
import com.bank.backend.boot.server.mapper.BankBaseMapper;
import com.bank.backend.boot.server.mapper.BankBranchMapper;
import com.bank.backend.boot.server.mapper.BankUserMapper;
import com.bank.backend.boot.server.service.BankUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* @author JiaoWei
* @description 针对表【bank_user】的数据库操作Service实现
* @createDate 2025-03-24 16:59:07
*/
@Slf4j
@Service
public class BankUserServiceImpl extends ServiceImpl<BankUserMapper, BankUser>
    implements BankUserService {


    @Resource
    private BankUserMapper bankUserMapper;

    @Resource
    private BankBaseMapper bankBaseMapper;

    @Resource
    private BankBranchMapper bankBranchMapper;

    @Resource
    private BankUserConverter bankUserConverter;

    @Override
    public BankUserVO getBankUserVO() {
        if (null == ThreadLocalUtil.get()) {
            throw new GlobalException("用户未登录");
        }
        Long uid = Long.parseLong(ThreadLocalUtil.get());
        BankUser bankUser = bankUserMapper.selectById(uid);
        if (null == bankUser) {
            throw new GlobalException("用户不存在");
        }
        BankBranch bankBranch = bankBranchMapper.selectById(bankUser.getBankBranchId());
        if (null == bankBranch) {
            throw new GlobalException("支行不存在");
        }
        BankBase bankBase = bankBaseMapper.selectById(bankBranch.getBankBaseId());
        if (null == bankBase) {
            throw new GlobalException("总行不存在");
        }
        BankInfoDTO bankInfoDTO = new BankInfoDTO();
        bankInfoDTO.setBankBaseName(bankBase.getName());
        bankInfoDTO.setBankBranchName(bankBranch.getName());
        bankInfoDTO.setLocation(bankBranch.getLocation());
        bankInfoDTO.setLevel(bankBranch.getLevel());
        BankUserVO bankUserVO = bankUserConverter.convert(bankUser);
        bankUserVO.setBankInfoDTO(bankInfoDTO);
        return bankUserVO;
    }

    /**
     * 根据id获取银行用户信息
     * @param id
     * @return
     */
    @Override
    public BankUserVO getBankUserByIdVO(Long id) {
        if (null == id) {
            throw new GlobalException("用户未登录");
        }
        BankUser bankUser = bankUserMapper.selectById(id);
        if (null == bankUser) {
            throw new GlobalException("用户不存在");
        }
        BankBranch bankBranch = bankBranchMapper.selectById(bankUser.getBankBranchId());
        if (null == bankBranch) {
            throw new GlobalException("支行不存在");
        }
        BankBase bankBase = bankBaseMapper.selectById(bankBranch.getBankBaseId());
        if (null == bankBase) {
            throw new GlobalException("总行不存在");
        }
        BankInfoDTO bankInfoDTO = new BankInfoDTO();
        bankInfoDTO.setBankBaseName(bankBase.getName());
        bankInfoDTO.setBankBranchName(bankBranch.getName());
        bankInfoDTO.setLocation(bankBranch.getLocation());
        bankInfoDTO.setLevel(bankBranch.getLevel());
        BankUserVO bankUserVO = bankUserConverter.convert(bankUser);
        bankUserVO.setBankInfoDTO(bankInfoDTO);
        return bankUserVO;
    }
}




